//app.js
const { user, getIsLogin, getNetUserInfo, clearInfo, userLog, sqLogin } = require('/lib/user.js')
const PAGES = require('/utils/pages.js')
App({
  //获取用户地理位置权限
  getPermission: function (obj) {
    wx.getSetting({
      success: function (res) {
        var statu = res.authSetting;
        if (!statu['scope.userLocation']) {
          wx.showModal({
            title: '是否授权当前位置',
            content: '需要获取您的地理位置，请确认授权，否则地图功能将无法使用',
            success: function (tip) {
              if (tip.confirm) {
                wx.openSetting({
                  success: function (data) {
                    if (data.authSetting["scope.userLocation"] === true) {
                      wx.showToast({
                        title: '授权成功',
                        icon: 'success',
                        duration: 1000
                      })
                      
                    } else {
                      wx.showToast({
                        title: '授权失败',
                        icon: 'success',
                        duration: 1000
                      })
                    }
                  }
                })
              }
            }
          })
        }
      },
      fail: function (res) {
        wx.showToast({
          title: '调用授权窗口失败',
          icon: 'success',
          duration: 1000
        })
      }
    })
  },
  onLaunch: function () {
    // 展示本地存储能力
    var logs = wx.getStorageSync('logs') || []
    logs.unshift(Date.now())
    wx.setStorageSync('logs', logs)

    var that=this;
    wx.getSystemInfo({
      success: function (res) {
        console.log(res.language)
        if (!wx.getStorageSync('Language')){
          wx.setStorageSync('Language', res.language == 'zh_CN' ? 'zh_CN' : 'en');
        }
        if (res.statusBarHeight > 20) {

          that.globalData.isFullSucreen = true

        }
      }
    })
    // 获取用户信息
    wx.getSetting({
      success: res => {
        if (res.authSetting['scope.userInfo']) {
          // 已经授权，可以直接调用 getUserInfo 获取头像昵称，不会弹框
          // 登录
          if(!getIsLogin){
            sqLogin();
          }else{
            wx.switchTab({
              url: PAGES.mainPage,
            })
          }
        }
      }
    })
  },
  globalData: {
    userInfo: null,
    tabSelected: 0,
    isFullSucreen: false, // 当前设备是否为 FullSucreen
    isShuaxinHome:false,//是否提交订单刷新首页
  }
})